﻿using System;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
using System.Drawing.Imaging;
using System.Text;
using System.Net;

public partial class ImageTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    public string[] GetFileList()
    {
        string[] downloadFiles;
        StringBuilder result = new StringBuilder();
        FtpWebRequest reqFTP;
        string ftpServerIP = "bd1.dc7.myftpupload.com";
        string ftpUserID = "bd1d3990";
        string ftpPassword = "Akoiced71#aosm";
        try
        {
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
                      "ftp://" + ftpServerIP + "/ArticalsImage/Full"));
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(ftpUserID,
                                                       ftpPassword);
            reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
            WebResponse response = reqFTP.GetResponse();
            StreamReader reader = new StreamReader(response
                                            .GetResponseStream());

            string line = reader.ReadLine();
            while (line != null)
            {
                result.Append(line);
                result.Append("\n");
                line = reader.ReadLine();
            }
            // to remove the trailing '\n'
            result.Remove(result.ToString().LastIndexOf('\n'), 1);
            reader.Close();
            response.Close();
            return result.ToString().Split('\n');
        }
        catch (Exception ex)
        {

            downloadFiles = null;
            return downloadFiles;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        string[] ImageListFile = GetFileList();
        for (int counter = 0; counter < ImageListFile.Length; counter++)
        {
            CompressJpeg(Server.MapPath("~/ArticalsImage/Full/LiveKora_39bb4d51-1d58-49d6-9a59-0144e124ae3e.jpg"), 55);
            //CompressJpeg(Server.MapPath("~/ArticalsImage/Full/" + ImageListFile[counter]), 55);
            div_ImageText.InnerHtml = div_ImageText.InnerHtml + ImageListFile[counter] + "---Done<br />";
        }
    }

    public void CompressJpeg(string path, int quality)
    {
        FileInfo objFileInfo;
        using (System.Drawing.Image source = System.Drawing.Image.FromFile(path))
        {
            ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders().First(c => c.MimeType == "image/jpeg");
            EncoderParameters parameters = new EncoderParameters(3);
            parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 40L);
            parameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.ScanMethod, (int)EncoderValue.ScanMethodInterlaced);
            parameters.Param[2] = new EncoderParameter(System.Drawing.Imaging.Encoder.RenderMethod, (int)EncoderValue.RenderProgressive);
            objFileInfo = new FileInfo(path);
            source.Save(Server.MapPath("~/ArticalsImage/x/") + objFileInfo.Name, codec, parameters);
        }

        File.Delete(Server.MapPath("~/ArticalsImage/Full/") + objFileInfo.Name);
        File.Move(Server.MapPath("~/ArticalsImage/x/") + objFileInfo.Name, Server.MapPath("~/ArticalsImage/Full/") + objFileInfo.Name);
        //File.Delete(Server.MapPath("~/ArticalsImage/x/") + objFileInfo.Name);



        //if (quality < 0 || quality > 100)
        //{
        //    throw new
        //        ArgumentOutOfRangeException("Quality must be between 0 and 100.");
        //}
        ////Creating temp. file and 
        //string tempFile = Server.MapPath("~/ArticalsImage/x/temp.dt");
        //File.Copy(path, tempFile, true);
        //using (var image = System.Drawing.Image.FromFile(tempFile))
        //{
        //    var qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
        //    var jpegCodec = ImageCodecInfo.GetImageEncoders()
        //        .Where(imageCodecInfo => imageCodecInfo.MimeType == "image/jpeg")
        //        .FirstOrDefault();
        //    var encoderParams = new EncoderParameters(1);
        //    encoderParams.Param[0] = qualityParam;

        //    image.Save(path, jpegCodec, encoderParams);


        //    var prevImageSize = new FileInfo(tempFile).Length;
        //    var nextImageSize = new FileInfo(path).Length;
        //    Console.WriteLine("Image compressed. Size saved :{0} bytes", prevImageSize - nextImageSize);
        //    //uploadFile();
        //}
        ////Removing the temp. file.
        //File.Delete(tempFile);
    }
}